package com.fuershiye.woman.monitor.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.beans.IdNameArea;
import com.fuershiye.woman.monitor.model.BulletinRead;
import com.google.common.collect.Lists;

@Repository("bulletinReadDao")
@SuppressWarnings("unchecked")
public class BulletinReadDao {

    @Autowired
    private HibernateDao hibernateDao;

    public void save(BulletinRead bulletinRead){
        hibernateDao.save(bulletinRead);
    }

    public BulletinRead get(Long id) {
        return hibernateDao.getById(BulletinRead.class,id);
    }

    public void delete(Long id) {
        String sql = "update BulletinRead set status = 0 where id = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(BulletinRead.class);
        query.setLong(0, id);
        query.executeUpdate();
    }
    
    @SuppressWarnings("unchecked")
    public List<BulletinRead> getByBidUid(Long bid, Long uid) {
    	String hql = "from BulletinRead where bid = :bid and uid = :uid ORDER BY rtime DESC";
    	Query query = hibernateDao.getQuery(hql);
		query.setLong("bid",bid);
		query.setLong("uid",uid);
    	return query.list();
    }
    
    @SuppressWarnings("unchecked")
    public List<BulletinRead> getByUid(Long uid) {
    	String hql = "from BulletinRead where uid = :uid ORDER BY rtime DESC";
    	Query query = hibernateDao.getQuery(hql);
    	query.setLong("uid",uid);
    	return query.list();
    }
    
    @SuppressWarnings("unchecked")
    public List<Long> getAllBidByUid(Long uid) {
    	String hql = "select id, bid from bulletin_read where uid = :uid ORDER BY rtime DESC";
    	Query query = hibernateDao.getSqlQuery(hql);
    	query.setLong("uid",uid);
    	
    	List<Long> result= Lists.newArrayList();
        List<Object[]> queryResult = query.list();
        for (Object[] fields : queryResult) {
        	result.add(fields[1] == null? 0:Long.parseLong(fields[1].toString()));
		}
    	return result;
    }
    
}
